package ua.kpi.transport.servlet.actions;

import ua.kpi.transport.servlet.actions.saving.SaveStationAction;
import ua.kpi.transport.servlet.actions.saving.SaveTypeAction;
import ua.kpi.transport.servlet.actions.editing.EditTypeAction;
import ua.kpi.transport.servlet.actions.editing.EditStationAction;
import ua.kpi.transport.servlet.actions.deleting.DeleteRouteAction;
import ua.kpi.transport.servlet.actions.deleting.DeleteTypeAction;
import ua.kpi.transport.servlet.actions.deleting.DeleteStationAction;
import ua.kpi.transport.servlet.actions.adding.AddStationAction;
import ua.kpi.transport.servlet.actions.adding.AddTypeAction;
import ua.kpi.transport.servlet.actions.lists.StationsListAction;
import ua.kpi.transport.servlet.actions.lists.RoutesListAction;
import ua.kpi.transport.servlet.actions.lists.TypesListAction;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.kpi.transport.servlet.actions.*;
import ua.kpi.transport.servlet.actions.adding.AddRouteAction;
import ua.kpi.transport.servlet.actions.adding.AddStationToRouteAction;
import ua.kpi.transport.servlet.actions.editing.EditRouteAction;
import ua.kpi.transport.servlet.actions.saving.SaveRouteAction;

/**
 *
 * @author harker777
 */
public class ActionExecutioner {

    private static ActionExecutioner instance;
    private HashMap<String, IAction> actions;

    private ActionExecutioner() {
        actions = new HashMap<String, IAction>();

        actions.put("defaultaction", new HomeAction());
        actions.put("home", new HomeAction());

        actions.put("stationslist", new StationsListAction());
        actions.put("addstation", new AddStationAction());
        actions.put("editstation", new EditStationAction());
        actions.put("savestation", new SaveStationAction());
        actions.put("deletestation", new DeleteStationAction());

        actions.put("typeslist", new TypesListAction());
        actions.put("addtype", new AddTypeAction());
        actions.put("edittype", new EditTypeAction());
        actions.put("savetype", new SaveTypeAction());
        actions.put("deletetype", new DeleteTypeAction());

        actions.put("routeslist", new RoutesListAction());
        actions.put("addroute", new AddRouteAction());
        actions.put("editroute", new EditRouteAction());
        actions.put("saveroute", new SaveRouteAction());
        actions.put("deleteroute", new DeleteRouteAction());
        actions.put("addstationtoroute", new AddStationToRouteAction());
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String actionName = request.getParameter("action");
        IAction action = actions.get(actionName);
        if (action == null) {
            action = actions.get("defaultaction");
        }
        return action.execute(request, response);
    }

    public static ActionExecutioner getInstance() {
        if (instance == null) {
            instance = new ActionExecutioner();
        }
        return instance;
    }
}
